<template>
    <div>
      <h1>Config1 配置管理</h1>
      <button @click="fetchConfig">刷新配置</button>
      <button @click="addProxy">添加代理</button>
      <ul>
        <li v-for="(proxy, index) in config.proxies" :key="index">
          <span>{{ proxy.name }}</span>
          <button @click="editProxy(index)">编辑</button>
          <button @click="deleteProxy(index)">删除</button>
          <ul>
            <li v-for="(location, locIndex) in proxy.locations" :key="locIndex">
              <span>{{ location.name }}</span>
              <button @click="editLocation(index, locIndex)">编辑</button>
              <button @click="deleteLocation(index, locIndex)">删除</button>
            </li>
          </ul>
        </li>
      </ul>
      <!-- 模态框用于添加和编辑 -->
      <div v-if="isModalOpen" class="modal">
        <div class="modal-content">
          <h2>{{ isEditing ? '编辑' : '添加' }} {{ isEditingLocation ? 'Location' : 'Proxy' }}</h2>
          <form @submit.prevent="submitForm">
            <label for="name">名称:</label>
            <input v-model="formData.name" type="text" id="name" required>
            <!-- 其他表单字段根据需要添加 -->
            <button type="submit">{{ isEditing ? '保存' : '添加' }}</button>
            <button @click="closeModal">取消</button>
          </form>
        </div>
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref, onMounted } from 'vue';
  import axios from 'axios';
  
  // 定义响应式数据
  const config = ref({});
  const isModalOpen = ref(false);
  const isEditing = ref(false);
  const isEditingLocation = ref(false);
  const formData = ref({});
  let currentProxyIndex = null;
  let currentLocationIndex = null;
  
  // 从 JSON 文件获取配置
  const fetchConfig = async () => {
    try {
      const response = await axios.get('/config1.json');
      config.value = response.data;
    } catch (error) {
      console.error('获取配置失败:', error);
    }
  };
  
  // 添加代理
  const addProxy = () => {
    isModalOpen.value = true;
    isEditing.value = false;
    isEditingLocation.value = false;
    formData.value = {};
  };
  
  // 编辑代理
  const editProxy = (index) => {
    isModalOpen.value = true;
    isEditing.value = true;
    isEditingLocation.value = false;
    formData.value = { ...config.value.proxies[index] };
    currentProxyIndex = index;
  };
  
  // 删除代理
  const deleteProxy = (index) => {
    config.value.proxies.splice(index, 1);
  };
  
  // 编辑 Location
  const editLocation = (proxyIndex, locIndex) => {
    isModalOpen.value = true;
    isEditing.value = true;
    isEditingLocation.value = true;
    formData.value = { ...config.value.proxies[proxyIndex].locations[locIndex] };
    currentProxyIndex = proxyIndex;
    currentLocationIndex = locIndex;
  };
  
  // 删除 Location
  const deleteLocation = (proxyIndex, locIndex) => {
    config.value.proxies[proxyIndex].locations.splice(locIndex, 1);
  };
  
  // 提交表单
  const submitForm = () => {
    if (isEditing.value) {
      if (isEditingLocation.value) {
        config.value.proxies[currentProxyIndex].locations[currentLocationIndex] = { ...formData.value };
      } else {
        config.value.proxies[currentProxyIndex] = { ...formData.value };
      }
    } else {
      if (isEditingLocation.value) {
        config.value.proxies[currentProxyIndex].locations.push({ ...formData.value });
      } else {
        config.value.proxies.push({ ...formData.value });
      }
    }
    closeModal();
  };
  
  // 关闭模态框
  const closeModal = () => {
    isModalOpen.value = false;
    isEditing.value = false;
    isEditingLocation.value = false;
    formData.value = {};
    currentProxyIndex = null;
    currentLocationIndex = null;
  };
  
  // 组件挂载时获取配置
  onMounted(() => {
    fetchConfig();
  });
  </script>
  
  <style scoped>
  .modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;
  }
  
  .modal-content {
    background-color: white;
    padding: 20px;
    border-radius: 5px;
  }
  </style>